# Copyright (c) 2011 Andrea Spadaccini <andrea.spadaccini@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

"""Utility file for creating in the datastore an Entity for the API key, that
will be manually changed by the admin."""

__author__ = "Andrea Spadaccini"
__license__ = "GPL v3 or later"
__email__ = "andrea.spadaccini@gmail.com"

import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from models import ApplicationData

class GenerateKey(webapp.RequestHandler):
    def get(self):
        key = ApplicationData.get_api_key()
        if not key:
            self.log("Creating the api_key Entity")
            k = ApplicationData(name = "api_key", value = "--")
            k.put()
        else:
            self.log("api_key Entity already in the datastore")
            self.log("%s, %s" % (str(key.name), str(key.value)))

    def log(self, msg):
        self.response.out.write(msg)
        logging.info(msg)

application = webapp.WSGIApplication([('/gen_key', GenerateKey)])

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
